/* 
 * File:   BlockingQueue.cpp
 * Author: lgr013
 * 
 * Created on 31 de octubre de 2014, 16:44
 */

#include "BlockingQueueEvent.h"

BlockingQueueEvent::BlockingQueueEvent() {
    //    this->pushing = false;
}

void BlockingQueueEvent::push(struct Event data) {

    std::lock_guard<std::mutex> lock(this->mtx);
    this->queue.push(data);

    this->cv.notify_all();
}

struct Event BlockingQueueEvent::pop() {

}

struct Event BlockingQueueEvent::waitAndPop() {
    std::unique_lock<std::mutex> uni_lock(this->mtx);

    cv.wait(uni_lock,[this]{return !this->queue.empty();});

    struct Event data = this->queue.front();
    this->queue.pop();
    
    return data;
}

bool BlockingQueueEvent::tryPop(struct Event data) {
    std::unique_lock<std::mutex> uni_lock(this->mtx);
    if (this->queue.empty()) {
        return false;
    }

    data = this->queue.front();
    this->queue.pop();
    return true;
}

bool BlockingQueueEvent::empty() {
    std::lock_guard<std::mutex> lock(this->mtx);
    return this->queue.empty();
}

int BlockingQueueEvent::size(){
    return this->queue.size();
}

BlockingQueueEvent::~BlockingQueueEvent() {
}

